# jsmud
# Copyright (C) 2008 Sean Middleditch

AC_INIT([jsmud], [0.6svn], [http://code.google.com/p/jsmud/issues/list])
AC_PREREQ([2.50])
AC_CONFIG_SRCDIR([src/main.c])

AC_HEADER_STDC

AC_ARG_WITH([zlib],
	[AS_HELP_STRING([--with-zlib=@<:@path@:>@],
		[specify the path where zlib is installed])],
	[CFLAGS="$CFLAGS -I$withval/include";LDFLAGS="$LDFLAGS -L$withval/lib"],
	[])
AC_CHECK_LIB([z], [inflate],, [AC_MSG_ERROR([fail])])
AC_CHECK_HEADER([zlib.h],, [AC_MSG_ERROR([can't find zlib.h])], [ ])

AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility])
AC_PATH_PROG([PKG_CONFIG], [pkg-config])
if test "x$PKG_CONFIG" = "x"; then
	AC_MSG_ERROR([can't find pkg-config])
fi

AC_MSG_CHECKING([for sqlite3])
if AC_RUN_LOG(["$PKG_CONFIG" --exists sqlite3]); then
	AC_MSG_RESULT([found])
	CFLAGS="$CFLAGS $("$PKG_CONFIG" --cflags sqlite3)"
	LIBS="$LIBS $("$PKG_CONFIG" --libs sqlite3)"
else
	AC_MSG_RESULT([not installed])
	"$PKG_CONFIG" --exists --print-errors sqlite3
	AC_MSG_ERROR([sqlite3 is required])
fi

AC_ARG_WITH([js],
	[AS_HELP_STRING([--with-js=@<:@path@:>@],
		[specify the path where SpiderMonkey is installed])],
	[PATH_JS="$withval"],
	[PATH_JS=""])

AC_ARG_VAR(JS_CONFIG, [path to js-config utility])
AC_PATH_PROG([JS_CONFIG], [js-config],, ["$PATH:$PATH_JS:$PATH_JS/bin"])
if test "x$JS_CONFIG" = "x"; then
	if test "x$PATH_JS" != "x"; then LDFLAGS="-L$PATH_JS/lib"; fi
	AC_CHECK_LIB([mozjs], [JS_NewContext], [LIBS="-lmozjs $LIBS";NEED_MOZJS="yes"],
		[AC_CHECK_LIB([js], [JS_NewContext], [LIBS="-ljs $LIBS";NEED_MOZJS="no"], 
			[AC_MSG_ERROR([can't find libjs or libmozjs])])])

	if test "x$NEED_MOZJS" = "xyes"; then
		AC_MSG_CHECKING([for jsapi.h])
		jsapi_found="no"
		for path in "$PATH_JS/mozjs" /usr/include/mozjs /usr/local/include/mozjs; do
			if test -f "$path/jsapi.h"; then
				AC_MSG_RESULT([$path/jsapi.h])
				CFLAGS="$CFLAGS -I$path"
				jsapi_found="yes"
				break
			fi
		done
		if test "x$jsapi_found" = "xno"; then
			AC_MSG_RESULT([not found])
			AC_MSG_ERROR([can't find jsapi.h])
		fi
	else
		AC_CHECK_HEADER([jsapi.h],, [AC_MSG_ERROR([can't find jsapi.h])], [#define XP_UNIX 1])
	fi
else
	CFLAGS="$CFLAGS $("$JS_CONFIG" --cflags)"
	LIBS="$LIBS $("$JS_CONFIG" --libs)"
fi

AC_CONFIG_FILES(config.mk)
AC_OUTPUT
